﻿// (c) Copyright Fernando Cerqueira.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Windows;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;

// ReSharper disable CheckNamespace
namespace WindowsPhoneCommands
// ReSharper restore CheckNamespace
{
    /// <summary>
    /// This class implements the tool window exposed by this package and hosts a user control.
    ///
    /// In Visual Studio tool windows are composed of a frame (implemented by the shell) and a pane, 
    /// usually implemented by the package implementer.
    ///
    /// This class derives from the ToolWindowPane class provided from the MPF in order to use its 
    /// implementation of the IVsUIElementPane interface.
    /// </summary>
    [Guid("5438a6b4-4fab-4a32-8e22-7cf82f65e556")]
    public class WpcMainToolWindow : ToolWindowPane
    {

        #region fields

        // This is the user control hosted by the tool window; it is exposed to the base class 
        // using the Content property. Note that, even if this class implements IDispose, we are
        // not calling Dispose on this object. This is because ToolWindowPane calls Dispose on 
        // the object returned by the Content property.
        private readonly FrameworkElement _control;

        #endregion

        #region constructor / Dispose

        /// <summary>
        /// Standard constructor for the tool window.
        /// </summary>
        public WpcMainToolWindow()
        {
            // Set the window title reading it from the resources.
            Caption = Resources.ToolWindowTitle;
            // Set the image that will appear on the tab of the window frame
            // when docked with an other window
            // The resource ID correspond to the one defined in the resx file
            // while the Index is the offset in the bitmap strip. Each image in
            // the strip being 16x16.
            BitmapResourceID = 301;
            BitmapIndex = 1;

            // This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
            // we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on 
            // the object returned by the Content property.
            _control = new MainControl(this);

        }

        /// <summary>
        /// This property returns the control that should be hosted in the Tool Window.
        /// It can be either a FrameworkElement (for easy creation of toolwindows hosting WPF content), 
        /// or it can be an object implementing one of the IVsUIWPFElement or IVsUIWin32Element interfaces.
        /// </summary>
        /// 
        override public object Content
        {
            get
            {
                return _control;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (_control != null)
            {
                if (((MainControl)_control).Timer != null)
                {
                    ((MainControl)_control).Timer.Stop();
                }
            }
            base.Dispose(disposing);
        }


        #endregion

        #region internal Methods

        internal void ShowInfoEnvDevice()
        {
            ((MainControl)_control).ShowInfoEnvDevice();
        }

        internal void UpdateMenu(bool allowedTools)
        {
            ((MainControl)_control).UpdateMenu(allowedTools);
        }

        internal void ResetBuild()
        {
            ((MainControl)_control).ResetBuild();
        }

        internal void StartSolutionBuild()
        {
            ((MainControl)_control).StartSolutionBuild();
        }

        internal void EndSolutionBuild(string msg)
        {
            ((MainControl)_control).EndSolutionBuild(msg);
        }

        internal void BuildSolutionStatus(string name, double currentPrj, int totPrj)
        {
            ((MainControl)_control).BuildSolutionStatus(name,currentPrj,totPrj);
        }

        internal void ConnectDebug()
        {
            ((MainControl)_control).ConnectDevice();
        }

        internal  void DisconenctDebug()
        {
            ((MainControl)_control).DisconnectDebug();
        }

        internal object GetServiceHelper(Type serviceType)
        {
            return base.GetService(serviceType);
        }

        #endregion

    }
}
